package com.song.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.common.base.UserContextInfo;
import com.song.common.response.PageInfo;
import com.song.framework.context.ContextUserInfoThreadHolder;
import com.song.framework.exception.ServiceException;
import com.song.module.system.entity.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.song.module.admin.request.ApCommentRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.song.module.admin.mapper.ApCommentMapper;
import org.springframework.beans.BeanUtils;
import com.song.module.admin.entity.ApComment;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.song.module.admin.service.IApCommentService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 用户评论业务层处理
 *
 * @author song
 * @date 2024-03-16
 */
@Slf4j
@Service
@AllArgsConstructor
public class ApCommentServiceImpl implements IApCommentService {

    private final ApCommentMapper baseMapper;

    private final ApCommentMapper apCommentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApComment add(ApCommentRequest request) {
        ApComment entity = ApComment.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ApCommentRequest request) {
        ApComment entity = ApComment.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public ApComment getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<ApComment> pageList(ApCommentRequest request) {
        Page<ApComment> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<ApComment> query = this.buildWrapper(request);
        IPage<ApComment> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<ApComment> list(ApCommentRequest request) {
        LambdaQueryWrapper<ApComment> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public void publish(ApCommentRequest request) {
        if (StringUtils.isEmpty(request.getContent())) {
            throw new ServiceException("评论内容不能为空");
        }
        UserContextInfo info = ContextUserInfoThreadHolder.get();
        ApComment apContent = ApComment.builder()
                .avatar(info.getAvatar())
                .userId(info.getId())
                .contentId(request.getContentId())
                .username(info.getUserName())
                .content(request.getContent())
                .build();
        baseMapper.insert(apContent);
        // 增加动态评论的数量
        apCommentMapper.updateCommentNum(request.getContentId());
    }

    private LambdaQueryWrapper<ApComment> buildWrapper(ApCommentRequest param) {
        LambdaQueryWrapper<ApComment> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getUsername())) {
            query.like(ApComment::getUsername, param.getUsername());
        }
        query.eq(Objects.nonNull(param.getContentId()), ApComment::getContentId, param.getContentId());
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(ApComment::getContent, param.getContent());
        }
        query.orderByDesc(BaseEntity::getCreateTime);
        return query;
    }

}
